package com.ziyougou.product.service.impl;

import com.ziyougou.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ziyougou.common.utils.PageUtils;
import com.ziyougou.common.utils.Query;

import com.ziyougou.product.dao.CategoryDao;
import com.ziyougou.product.entity.CategoryEntity;
import com.ziyougou.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    @Override
    public Long[] findParentPathByCategoryId(Long catelogId) {
        //1.创建集合保存最终结果
        List<Long> idpath=new ArrayList<Long>();
        //2.调取方法根据分类id,查询分类的父元素
        List<Long> path=findPath(catelogId,idpath);//这里返回的数据是三级菜单+二级菜单+一级菜单
        //3.将集合中的数据做反转
        Collections.reverse(path);
        Long[] ids= new Long[path.size()];
        return path.toArray(ids);
    }

    //参数1 分类id  参数2 保存id的集合
    private List findPath(Long catelogId, List<Long> idpath) {
        idpath.add(catelogId);
        CategoryEntity category = baseMapper.selectById(catelogId);
        if(category.getParentCid()!=0){
            findPath(category.getParentCid(),idpath);
        }
        return  idpath;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    //构建树形结构
    //1.查询出所有的分类目录
    //2.根据parentid和level(等级)的值进行树化(分级)-涉及技能:递归
    @Override
    public List<CategoryEntity> listWithTree() {
        //1、查出所有分类
        List<CategoryEntity> entities = baseMapper.selectList(null);
        //2、组装成父子的树形结构
        //2.1）找到所有的一级分类(知识点:map()和filter()的区别)
        List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
                categoryEntity.getParentCid() == 0
        ).map((menu)->{
            menu.setChildren(getChildrens(menu,entities));
            return menu;
        }).sorted((menu1,menu2)->{
            //这里做非空校验的原因是sort是Integer类型，不做非空会有空指针异常
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());
        return level1Menus;
    }

    //递归查找所有菜单的子菜单
    private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){

        List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map(categoryEntity -> {
            //1、找到子菜单
            categoryEntity.setChildren(getChildrens(categoryEntity,all));
            return categoryEntity;
        }).sorted((menu1,menu2)->{
            //2、菜单的排序
            return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
        }).collect(Collectors.toList());

        return children;
    }
}